class FriendShipsController < ApplicationController
  layout "base.html", :only => :index

  def index
    @html_title = '好友'
    @html_description = '好友'
    @friends = current_valleyer.all_of_my_friends
    respond_to do |format|
      format.html do
        
      end
      format.js do
        render :layout => false
      end
    end
  end

  def my_friends
    #初始化变量
    if params[:nick_name].blank?
      nick_name = "%"
    else
      nick_name = params[:nick_name]
    end
    @friends = current_valleyer.all_friends_by_term({:nick_name => nick_name, :sex => params[:sex]})
  end

  def not_my_friends
    if params[:friend_id].nil?
      #初始化变量
      if params[:nick_name].blank?
        nick_name = "%"
      else
        nick_name = params[:nick_name]
      end
      @friends = current_valleyer.all_with_out_my_friends({:nick_name => nick_name, :sex => params[:sex]})
      @nick_name = nick_name
      @sex = params[:sex]
    else
      @nick_name = params[:nick_name]
      @sex = params[:sex]
      @make_friend_requisition = WaitMakeFriendRequisition.new :active_valleyer_id => current_valleyer_id,
        :passive_valleyer_id => params[:friend_id]
      @make_friend_requisition.save
      @friends = current_valleyer.all_with_out_my_friends({:nick_name => params[:nick_name], :sex => params[:sex]})
    end
  end

  def ask_for_pals
    @wait_requisitions = WaitMakeFriendRequisition.wait_me_to_confirm current_valleyer_id
    if params[:cmd] == "agree"
      requisition = WaitMakeFriendRequisition.find params[:wait_requisition_id]
      requisition[:type] = AgreeMakeFriendRequisition.to_s
      requisition.save
      @my_friend_ship = FriendShip.new :valleyer_id => current_valleyer_id,
        :friend_id => requisition.active_valleyer_id,
        :friend_group_id => FriendGroup.find_by_name("未分组").id
      @my_friend_ship.save
      @you_friend_ship = FriendShip.new :valleyer_id => requisition.active_valleyer_id,
        :friend_id => current_valleyer_id,
        :friend_group_id => FriendGroup.find_by_name("未分组").id
      @you_friend_ship.save
      else if params[:cmd] == "reject"
        requisition = WaitMakeFriendRequisition.find params[:wait_requisition_id]
        requisition[:type] = RejectMakeFriendRequisition.to_s
        requisition.save
      end
    end
    @wait_requisitions = WaitMakeFriendRequisition.wait_me_to_confirm current_valleyer_id
  end

  def delete_friend
    @delete_friend_requisition = AgreeMakeFriendRequisition.first :conditions => [
      "active_valleyer_id = ? and passive_valleyer_id = ? ",
      current_valleyer_id,
      params[:friend_id]
    ]
    if @delete_friend_requisition
      @delete_friend_requisition.destroy
    else
      @delete_friend_requisition = AgreeMakeFriendRequisition.first :conditions => [
        "active_valleyer_id = ? and passive_valleyer_id = ? ",
        params[:friend_id],
        current_valleyer_id
      ]
      @delete_friend_requisition.destroy
    end
    @current_friend_ship = FriendShip.find_by_valleyer_id_and_friend_id current_valleyer_id, params[:friend_id]
    @delete_friend_ship = FriendShip.find_by_valleyer_id_and_friend_id params[:friend_id], current_valleyer_id
    @current_friend_ship.destroy
    @delete_friend_ship.destroy
    @friends = current_valleyer.all_of_my_friends
  end
end
